package com.ncmem.up6.database;

import com.google.gson.Gson;
import com.ncmem.down2.model.DnFileInf;
import com.ncmem.up6.SpringUtil;
import com.ncmem.up6.model.FileInf;
import com.ncmem.up6.model.ObsKeyErr;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

public class DbFileMongodb extends DBFile
{
    private MongoTemplate mongo;

    public DbFileMongodb(){
        this.mongo = SpringUtil.getBean(MongoTemplate.class);
    }

    public String GetAllUnComplete(int f_uid){

        List<FileInf> fs = this.mongo.findAll(FileInf.class,"up6_files");

        Gson g = new Gson();
        return g.toJson( fs );
    }

    public FileInf read(String f_id){
        return this.mongo.findById(f_id,FileInf.class,"up6_files");
    }

    public boolean exist_file(String md5,/*out*/FileInf fileSvr){

        Query q = new Query(Criteria.where("md5").is(md5));
        List<FileInf> fs = this.mongo.find(q,FileInf.class);
        if(!fs.isEmpty())
        {
            fileSvr = fs.get(0);
        }

        return !fs.isEmpty();
    }

    public void Add(FileInf f){
        FileInf s = new FileInf();
        s.copy(f);
        //将minio_id改为obs key
        s.minio_id = s.ObsKey();
        this.mongo.insert(s);
    }

    public void AddObsKeyErr(ObsKeyErr k){
        this.mongo.insert(k);
    }

    /**
     * 清空数据集
     */
    public void Clear(){
        Query query = new Query(Criteria.where("complete").is(true)
                .orOperator(Criteria.where("complete").is(false)));
        List<FileInf> fs = this.mongo.findAll(FileInf.class);
        for(int i = 0 , l = fs.size() ; i < l ; ++i)
        {
            FileInf f = (FileInf)fs.get(i);
            this.mongo.remove(f);
        }
        System.out.println("文件数据集清空完毕");

        fs = this.mongo.findAll(FileInf.class, "up6_folders");
        for(int i = 0 , l = fs.size() ; i < l ; ++i)
        {
            FileInf f = (FileInf)fs.get(i);
            this.mongo.remove(f);
        }

        System.out.println("目录数据集清空完毕");
    }


    public boolean f_process(int uid,String f_id,long offset,long f_lenSvr,String f_perSvr)
    {
        Query q = new Query(Criteria.where("id").is(f_id));
        Update up = new Update();
        up.set("offset",offset)
                .set("lenSvr",f_lenSvr)
                .set("perSvr",f_perSvr);
        this.mongo.updateFirst(q,up,FileInf.class,"up6_files");

        return  true;
    }

    public void complete(String id)
    {
        Query q = new Query(Criteria.where("id").is(id));
        Update up = new Update();
        up.set("complete",true)
                .set("scaned",true)
                .set("perSvr","100");
        this.mongo.updateFirst(q, up, FileInf.class,"up6_files");
    }

    public void Delete(int f_uid,String f_id)
    {
        Query q = new Query(Criteria.where("id").is(f_id)
                .orOperator(Criteria.where("f_id").is(f_id)));
        this.mongo.remove(q,FileInf.class,"up6_files");
    }

    public void delete(String pid,String name,int uid,String id)
    {
    }
}
